#include "StdAfx.h"

#include <PSDFCore/Event/EventHandler.h>

void EventHandler::handle( unsigned eventType, int param1, int param2 )
{
	// Can not guarantee the two handle-event methods overloaded by user are reentrant, so a mutex is used.
	bool locked = _handlerMutex.tryLock();
	if( !locked )
	{
		for (int i = 0; i < 10; ++i)
		{
			// Prompt for 10 times (20ms in total).
			PosixThread::msleep(2);
			locked = _handlerMutex.tryLock();
			if (locked) { break; }
		}
		if (!locked) { return; }
	}
	if( eventType & SYSTEM_EVENT_GROUP )
	{
		handleSystemEvent( eventType, param1, param2 );
	}
	else
	{
		handleCustomEvent( eventType, param1, param2 );
	}
	_handlerMutex.unlock();
}

void EventHandler::handleSystemEvent( unsigned int eventType, int param1, int param2 )
{
	TOUCH( eventType );
	TOUCH( param1 );
	TOUCH( param2 );
}
	
void EventHandler::handleCustomEvent( unsigned eventType, int param1, int param2 )
{
	TOUCH( eventType );
	TOUCH( param1 );
	TOUCH( param2 );
}